home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-08-20 | 14.4 KB | 541 lines | [TEXT/MPS ] |
- // Copyright © 1995 by Apple Computer, Inc. All rights reserved.
- // PCISlots.cp
-
- /*
- FILE
- PCISlots.cp
-
- NAME
- PCI Slot Peek source file
-
- DESCRIPTION
- This source file implements the PCI Slot Peek application.
-
- MODIFICATION HISTORY
- Created by Terry Teague
-
- 20 Apr 95 - TRT - Initial version
- 01 Jun 95 - TRT - Add support for finding slot # given driver refNum.
- Provide separate and generic setup/teardown routines for PPC native code
- 12 Jun 95 - TRT - Changed error handling for PCI on machines that don't have the slots
- but do have NameRegistry support in ROM/System software
- 17 Jul 95 - TRT - Tidy-up source for PPCC v1.0.5, Universal Headers v2.0 final,
- CodeWarrior 6.1.
-
- =============================================================================================
- */
-
- //================================================================================================
- // Build-time #defines
- //================================================================================================
-
- // pre-Universal Headers (<MPW 3.3.1) compatibility
- #ifndef qPreUniHeaders
- #define qPreUniHeaders 0
- #endif
-
- //================================================================================================
- // Standard Includes
- //================================================================================================
-
- #include <Types.h>
- #include <Packages.h>
- #include <Dialogs.h>
- #include <Memory.h>
- //#if qPreUniHeaders
- //#include <OSEvents.h>
- //#endif
- #include <QuickDraw.h>
- #include <Resources.h>
- #include <ToolUtils.h>
- #include <StdIO.h>
- #include <Slots.h>
- #include <ROMDefs.h>
- #include <Devices.h>
- #include <Traps.h>
- #include <String.h>
- #include <Strings.h>
- #include <CType.h>
- #if qPreUniHeaders
- #include <SysEqu.h>
- #else
- #include <LowMem.h>
- #endif
- #include <Power.h>
- #include <DeskBus.h>
-
- // v2.0 final or later
- #ifndef __CODEFRAGMENTS__
- #include <CodeFragments.h>
- #endif
-
- // v2.0a3 or earlier (also kept in v2.0 final for compatibility)
- //#ifndef __FRAGLOAD__
- //#include <FragLoad.h>
- //#endif
-
- #ifndef __MIXEDMODE__
- #include <MixedMode.h>
- #endif
-
- // v2.0a3 or earlier
- //#ifndef __GESTALTEQU__
- //#include <GestaltEqu.h>
- //#endif
-
- // v2.0 final or later
- #ifndef __GESTALT__
- #include <Gestalt.h>
- #endif
-
- // v2.0a3 or earlier
- #ifdef __FRAGLOAD__
- typedef SInt32 OSStatus;
- #endif
-
- #ifndef __NAMEREGISTRY__
- #include <NameRegistry.h>
- #endif
-
- //================================================================================================
- // PCISlots Specific Includes
- //================================================================================================
-
- #ifndef __PCISLOTS__
- #include "PCISlots.h"
- #endif
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- #if GENERATINGPOWERPC || defined(powerc) || defined (__powerc)
- #pragma options align=mac68k
- #endif
-
- #ifdef __CFM68K__
- #pragma lib_export on
- #endif
-
- //================================================================================================
- // Global variables
- //================================================================================================
-
- #if GENERATINGPOWERPC || defined(powerc) || defined (__powerc)
-
- #else
-
- // 68K code only
- // these have to be A5 based global variables
-
- RegistryEntryIterateCreateUPP myRegistryEntryIterateCreateUPP;
- RegistryEntryIterateDisposeUPP myRegistryEntryIterateDisposeUPP;
- RegistryEntryIterateUPP myRegistryEntryIterateUPP;
- RegistryEntryIDDisposeUPP myRegistryEntryIDDisposeUPP;
- RegistryPropertyGetSizeUPP myRegistryPropertyGetSizeUPP;
- RegistryPropertyGetUPP myRegistryPropertyGetUPP;
- RegistryEntryToPathSizeUPP myRegistryEntryToPathSizeUPP;
- RegistryCStrEntryToPathUPP myRegistryCStrEntryToPathUPP;
-
- #endif
-
- //================================================================================================
- // Setup PPC native code glue
- //================================================================================================
-
- #if GENERATINGPOWERPC || defined(powerc) || defined (__powerc)
-
- #else
-
- // 68K code only
-
- OSErr SetupPPCNativeCode(CFragConnectionID *connID, Handle *PEFHandle)
- {
- OSErr myErr;
-
- long templong;
-
- Boolean hasCFM;
- Boolean hasMixedMode;
- CFragSymbolClass myClass;
- Str255 failedFragName;
-
- //DebugStr("\pAbout to setup PPC native code");
-
- myErr = noErr;
-
- *PEFHandle = Get1Resource(kPEFResType, kPEFresourceID); // get PEF container resource from our resource fork
- if (*PEFHandle && !(myErr = ResError()))
- {
- // Check for Mixed Mode Manager and Code Fragment Manager (CFM)
-
- hasMixedMode = !Gestalt(gestaltMixedModeAttr, &templong);
- hasCFM = !Gestalt(gestaltCFMAttr, &templong);
-
- myErr = Gestalt(gestaltSysArchitecture, &templong);
- if (myErr || (templong == gestalt68k))
- {
- ReleaseResource(*PEFHandle);
-
- return (glueNotPPCErr);
- }
-
- if (!hasCFM || !hasMixedMode)
- {
- ReleaseResource(*PEFHandle);
-
- return (glueNoCFMorMMErr);
- }
-
- DetachResource(*PEFHandle);
- MoveHHi(*PEFHandle);
- HLock(*PEFHandle);
-
- // Assume this is PowerPC code, so it must be "prepared"
- myErr = GetMemFragment((Ptr)**PEFHandle, 0, NULL, kNewCFragCopy, connID, NULL, failedFragName);
- if (myErr)
- {
- //DebugStr(failedFragName); // at present
- DisposeHandle(*PEFHandle);
-
- return (glueCFMPrepareErr);
- }
-
- myErr = FindSymbol(*connID, (ConstStr255Param)"\pRegistryEntryIterateCreateRD", (Ptr *)&myRegistryEntryIterateCreateUPP, &myClass);
- if (!myErr)
- myErr = FindSymbol(*connID, (ConstStr255Param)"\pRegistryEntryIterateDisposeRD", (Ptr *)&myRegistryEntryIterateDisposeUPP, &myClass);
- if (!myErr)
- myErr = FindSymbol(*connID, (ConstStr255Param)"\pRegistryEntryIterateRD", (Ptr *)&myRegistryEntryIterateUPP, &myClass);
- if (!myErr)
- myErr = FindSymbol(*connID, (ConstStr255Param)"\pRegistryEntryIDDisposeRD", (Ptr *)&myRegistryEntryIDDisposeUPP, &myClass);
- if (!myErr)
- myErr = FindSymbol(*connID, (ConstStr255Param)"\pRegistryPropertyGetSizeRD", (Ptr *)&myRegistryPropertyGetSizeUPP, &myClass);
- if (!myErr)
- myErr = FindSymbol(*connID, (ConstStr255Param)"\pRegistryPropertyGetRD", (Ptr *)&myRegistryPropertyGetUPP, &myClass);
- if (!myErr)
- myErr = FindSymbol(*connID, (ConstStr255Param)"\pRegistryEntryToPathSizeRD", (Ptr *)&myRegistryEntryToPathSizeUPP, &myClass);
- if (!myErr)
- myErr = FindSymbol(*connID, (ConstStr255Param)"\pRegistryCStrEntryToPathRD", (Ptr *)&myRegistryCStrEntryToPathUPP, &myClass);
-
- if (myErr)
- {
- //DebugStr("\pa NameRegistryLib symbol could not be found"); // at present
-
- DisposeHandle(*PEFHandle);
-
- return (glueFindSymbolErr);
- }
-
- } else
-
- {
- myErr = glueNoGlueErr;
- }
-
- return (myErr);
-
- } /* setupppcnativecode */
-
- //================================================================================================
- // Tear down PPC native code glue
- //================================================================================================
- void TearDownPPCNativeCode(CFragConnectionID connID, Handle PEFHandle)
- {
- #if 0
- // don't need to close the connection?
- if (myRegistryEntryIterateCreateUPP)
- DisposeRoutineDescriptor(myRegistryEntryIterateCreateUPP);
- if (myRegistryEntryIterateDisposeUPP)
- DisposeRoutineDescriptor(myRegistryEntryIterateDisposeUPP);
- if (myRegistryEntryIterateUPP)
- DisposeRoutineDescriptor(myRegistryEntryIterateUPP);
- if (myRegistryEntryIDDisposeUPP)
- DisposeRoutineDescriptor(myRegistryEntryIDDisposeUPP);
- if (myRegistryPropertyGetSizeUPP)
- DisposeRoutineDescriptor(myRegistryPropertyGetSizeUPP);
- if (myRegistryPropertyGetUPP)
- DisposeRoutineDescriptor(myRegistryPropertyGetUPP);
- if (myRegistryEntryToPathSizeUPP)
- DisposeRoutineDescriptor(myRegistryEntryToPathSizeUPP);
- if (myRegistryCStrEntryToPathUPP)
- DisposeRoutineDescriptor(myRegistryCStrEntryToPathUPP);
- #endif
-
- CloseConnection(&connID);
-
- DisposeHandle(PEFHandle);
-
- } /* teardownppcnativecode */
-
- #endif
-
- // I could have implemented the same code for 68K and native PPC
- // if I had used macros as per PCISlots.h, but I didn't
-
- #if GENERATINGPOWERPC || defined(powerc) || defined (__powerc)
-
- //================================================================================================
- // Get PCI slot # for specified driver refNum (not used in this application)
- //================================================================================================
- short GetPCISlotNumber(short refNum)
- {
- short slotNum;
-
- slotNum = 0;
-
- slotNum = GetSlotForThisRefNum(refNum);
-
- return (slotNum);
-
- } /* getpcislotnumber */
-
- #else
-
- // 68K code only
-
- //================================================================================================
- // Get PCI slot # for specified driver refNum (not used in this application)
- //================================================================================================
- short GetPCISlotNumber(short refNum)
- {
- OSErr myErr;
- CFragConnectionID connID;
- Handle PEFHandle;
- short slotNum;
-
- slotNum = 0;
-
- if ((myErr = SetupPPCNativeCode(&connID, &PEFHandle)) == noErr)
- {
- // 68K application calling native PPC code at this point
-
- //DebugStr("\pAbout to call 68K->PPC glue code");
- slotNum = GetSlotForThisRefNum(refNum);
-
- TearDownPPCNativeCode(connID, PEFHandle);
- } else
-
- {
- // handle errors when trying to set up PPC native glue code
- }
-
- return (slotNum);
-
- } /* getpcislotnumber */
-
- #endif
-
- //================================================================================================
- // PCISlots main program
- //================================================================================================
-
- int main(void)
- {
- OSErr theErr;
- short numSlots;
-
- long templong;
- char tempstr[256];
-
- #if GENERATINGPOWERPC || defined(powerc) || defined (__powerc)
-
- // native PPC application only
-
- // If the NameRegistryLib is weak-linked, and it is missing, we need to
- // check for the library, to prevent a crash if we try to access the library's exports;
- // currently Finder reports the library is missing
-
- #if 1
- CFragConnectionID connID;
- Ptr mainAddr;
- Str255 errName;
-
- getindstring(tempstr, kMiscPCISTRID, kPCISlotsTitle); // formatting string
- printf("%s", tempstr);
-
- theErr = Gestalt(gestaltCFMAttr, &templong);
- if (!theErr)
- {
- theErr = GetSharedLibrary((ConstStr63Param)"\pNameRegistryLib", kAnyCFragArch, kFindCFrag, &connID, &mainAddr, errName);
- }
- #endif
-
- if (theErr)
- {
- // No Code Fragment Manager, hence no shared library
- // or the shared library is missing
-
- getindstring(tempstr, kMiscPCISTRID, kNoNameRegistryLib); // formatting string
- printf("%s", tempstr);
-
- return (1);
- }
-
- //DebugStr((const unsigned char*)"\pAbout to do NameRegistry Gestalt call");
- theErr = Gestalt(gestaltNameRegistryVersion, &templong);
- if (theErr)
- {
- getindstring(tempstr, kMiscPCISTRID, kNoPCISlots); // formatting string
- printf("%s", tempstr);
- return (1);
- }
-
- numSlots = EnumerateNameRegistry();
-
- if (!numSlots)
- {
- getindstring(tempstr, kMiscPCISTRID, kNoCardsInPCISlots); // formatting string
- printf("%s", tempstr);
- }
-
- return (0);
-
- #else
-
- // 68K application only
-
- Boolean hasPCI;
- CFragConnectionID connID;
- Handle PEFHandle;
-
- //DebugStr("\pAbout to get PCI Info");
-
- if ((theErr = SetupPPCNativeCode(&connID, &PEFHandle)) == noErr)
- {
- // Output title
-
- getindstring(tempstr, kMiscPCISTRID, kPCISlotsTitle); // formatting string
- printf("%s", tempstr);
-
- // If the NameRegistryLib is weak-linked, and it is missing, we need this
- // Gestalt check anyway, to prevent a crash if we try to access the library's exports
-
- //DebugStr((const unsigned char*)"\pAbout to do NameRegistry Gestalt call");
- hasPCI = !Gestalt(gestaltNameRegistryVersion, &templong);
-
- theErr = Gestalt(gestaltSysArchitecture, &templong);
- if (theErr || (templong == gestalt68k) || !hasPCI)
- {
- getindstring(tempstr, kMiscPCISTRID, kNoPCISlots); // formatting string
- printf("%s", tempstr);
-
- TearDownPPCNativeCode(connID, PEFHandle);
-
- return (1);
- }
-
- // 68K application calling native PPC code at this point
-
- numSlots = EnumerateNameRegistry();
-
- TearDownPPCNativeCode(connID, PEFHandle);
-
- if (!numSlots)
- {
- getindstring(tempstr, kMiscPCISTRID, kNoCardsInPCISlots); // formatting string
- printf("%s", tempstr);
- }
-
- return (0);
-
- } else
-
- if (theErr != glueNoGlueErr)
- {
- // handle errors when trying to set up PPC native glue code - at present
-
- short tempint;
-
- tempint = 0;
-
- switch (theErr) {
- case glueNotPPCErr : tempint = kNoPCISlots;
- break;
- case glueNoCFMorMMErr : tempint = kNo68KNameRegistry;
- break;
- case glueCFMPrepareErr : tempint = kNoNameRegistryLib;
- break;
- case glueFindSymbolErr : tempint = kNoNameRegistryLib;
- break;
- default : tempint = kNoNameRegistryLib;
- break;
- } // switch
-
- if (tempint)
- {
- // Output title
-
- getindstring(tempstr, kMiscPCISTRID, kPCISlotsTitle); // formatting string
- printf("%s", tempstr);
-
- getindstring(tempstr, kMiscPCISTRID, tempint); // formatting string
- printf("%s", tempstr);
- }
- } else
-
- {
-
- long Architecture;
- short itemHit;
- AlertTHndl theAlertTemplate;
- Handle theDITLHandle;
- Str255 theMessage;
-
- // There is no native PPC code available, or we are running on a true 68K machine
-
- InitGraf((Ptr)&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- // set cursor to an arrow
- SetCursor(&qd.arrow);
-
- // pre-flight everything first
-
- theAlertTemplate = (AlertTHndl)GetResource('ALRT', kNotPPCResourceID); // is the ALRT resource around?
- if (theAlertTemplate)
- theDITLHandle = GetResource('DITL', (*theAlertTemplate)->itemsID); // How about the DITL?
-
- theErr = Gestalt(gestaltSysArchitecture, &Architecture); // Which message to print?
- if (theErr || (Architecture == gestalt68k) )
- GetIndString(theMessage, kNotPPCResourceID, kMac68KMsgID);
- else
- GetIndString(theMessage, kNotPPCResourceID, kPowerMacMsgID);
-
- if ((theAlertTemplate) && (theDITLHandle) && (theMessage[0] != 0))
- {
- // Success at last...
-
- ParamText(theMessage, (ConstStr255Param) "\p", // Use the loaded STR resource to replace
- (ConstStr255Param) "\p", (ConstStr255Param) "\p"); // ^0 in alert's DITL.
- itemHit = Alert(kNotPPCResourceID, nil); // Run the alert.
- } else
-
- {
- // give some indication we're hosed...
- SysBeep(2);
- }
-
- return (1);
-
- }
- #endif
-
- } /* main */
-
- #ifdef __CFM68K__
- #pragma lib_export off
- #endif
-
- #if GENERATINGPOWERPC || defined(powerc) || defined (__powerc)
- #pragma options align=reset
- #endif
-
- #ifdef __cplusplus
- }
- #endif
-